{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7759922b-12c9-44e0-8ac3-5f2a02b321d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fitz  # PyMuPDF\n",
    "import os\n",
    "import requests\n",
    "import json\n",
    "from typing import List\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display, update_display\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a266273a-05e3-451e-a318-428726cfa39c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize and constants\n",
    "\n",
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
    "    print(\"API key looks good so far\")\n",
    "else:\n",
    "    print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n",
    "    \n",
    "MODEL = 'gpt-4o-mini'\n",
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45566572-dd66-48dc-ab7b-6adbe26eacba",
   "metadata": {},
   "outputs": [],
   "source": [
    "exceptions = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "effc0e7b-d668-48b3-86d0-dbb5d8fe3d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Building system prompt\n",
    "def get_verse_system_prompt():\n",
    "    system_prompt = \"You are a spiritual student who classifies the versus of the BhagavadGita according to a given theme.\\n\"\n",
    "    system_prompt += \"Given a theme, you should pick a verse from any chapter and give it's location in the form of index chapter.verse_number (6.2)\\n\"\n",
    "    system_prompt += \"You should respond in JSON as in this example:\\n\"\n",
    "    system_prompt += \"\"\"\n",
    "        {\"title\": \"Chapter 3, Verse 21 (3.21)\", \"verse\": \"कर्मणा ह्यपि संसिद्धिम्‌\n",
    "                                                  आस्थिता जनकादय:।\n",
    "                                                  लोकसंग्रहमेवापि\n",
    "                                                  सम्पश्यन्कर्तुमर्हसि॥\"}\n",
    "        \"\"\"\n",
    "    return system_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbfb1035-b183-4481-9b49-3cc1b12b42e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(get_verse_system_prompt())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6acdcd6c-1fc5-4c71-81d0-665e25808e46",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define user prompt\n",
    "def get_verse_user_prompt(theme):\n",
    "    user_prompt = f'''\n",
    "                    Here is the theme : {theme},\n",
    "                    Please find a verse from BhagavadGita excluding {exceptions} for a given theme {theme}\n",
    "                    '''#excluding those results which are already used\n",
    "    \n",
    "    user_prompt += \"If the verse is not in the exceptions for a given theme and used for a different theme, you are free to suggest it for a different theme.\"\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f5c755-ec2d-4545-9a31-0f6b2e5ed4da",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(get_verse_user_prompt('motivation'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "304d432c-7216-4a90-a5d8-db36b193657d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Call openAI to return versus\n",
    "def get_verses(theme):\n",
    "    response = openai.chat.completions.create(\n",
    "        model=MODEL,\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": get_verse_system_prompt()},\n",
    "            {\"role\": \"user\", \"content\": get_verse_user_prompt(theme)}\n",
    "      ],\n",
    "        response_format={\"type\": \"json_object\"}\n",
    "    )\n",
    "    result = response.choices[0].message.content\n",
    "    result = json.loads(result)\n",
    "\n",
    "    #Remember those results which are suggested now\n",
    "    combination = (theme, result['title'])\n",
    "    exceptions.append(combination)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b90eeb35-e10e-48ee-ade6-e0594da8c51b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(get_verses('motivation'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5b8925e-52e4-4cb7-9205-51c65ed88fb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# So far we have fetched the new verses relevant to a given theme \n",
    "# Lets generate a script for producting youtube video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ff0862b-0310-4174-ad12-64047932dc9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#def function for system prompt\n",
    "def get_script_system_prompt(tone, theme, format):\n",
    "    sys_prompt = 'You are a script writer for a youtube spiritual channel\\n'\n",
    "    sys_prompt += 'You are given a verse like below: \\n'\n",
    "    sys_prompt += str(get_verses(theme))\n",
    "    sys_prompt += '\\n'\n",
    "    sys_prompt += f'Give me an engaging script in a {tone} tone for a {format} format video for audience like youth seeking purpose, spiritual seekers, indians abroad, scholars and curious minds.'\n",
    "\n",
    "    return sys_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47476516-cd2f-4b16-b378-a70617bbe284",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(get_script_system_prompt('Motivating','motivation','long'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e305525b-8dde-4e93-927a-e24531827498",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def function for user prompt\n",
    "def get_script_user_prompt(format, theme):\n",
    "    user_prompt = f'Given the verse, help me generate a detailed script suitable for {format} format video.\\n'\n",
    "    user_prompt += f'Please give me the complete verse, its meaning, a relevant story having a dilemma which the verse solves and the interpretation of the verse with respect to {theme}.\\n'\n",
    "    user_prompt += 'Let the script give cues about video editing, host actions.'\n",
    "    user_prompt += 'given the below example, please follow the format:\\n'\n",
    "    user_prompt += \"\"\"\n",
    "    [Opening Scene - Soft Instrumental Music Playing]\n",
    "\n",
    "    [Cut to Host in a serene setting, perhaps by a river or in a lush green garden.]\n",
    "\n",
    "    Host: (Smiling at the camera) \"Namaste, dear viewers! Welcome back to our channel, where we explore the depths of spirituality and seek to ignite the flame of wisdom within you. Today, we delve into a profound verse from the Bhagavad Gita that speaks to the very essence of life and identity.\"\n",
    "\n",
    "    [Text On Screen: Chapter 2, Verse 13 (2.13)]\n",
    "\n",
    "    Host: (With a sense of reverence) \"Let’s first take a moment to recite this verse together. It goes like this:\n",
    "\n",
    "    देहिनोऽस्मिन्न्यथा देहे कौमारं यौवनं जरा।\n",
    "    तथादेहान्तरप्राप्तिर्धीरस्तत्र न मुह्यति॥\n",
    "\n",
    "    Now, let’s understand the essence of this verse.\"\n",
    "\n",
    "    [Cut to Graphic: Verse Translation with Key Concepts Highlighted]\n",
    "\n",
    "    Host Voiceover: (Calm and engaging tone) \"The meaning of this beautiful verse translates to: 'Just as the body undergoes changes from childhood to old age, similarly, the soul transitions from one body to another. The wise, who understand this, are never bewildered by these changes.'\n",
    "\n",
    "    [Cut back to Host]\n",
    "\n",
    "    Host: (Nodding, creating a connection)\n",
    "    \"So, why is this verse so important, especially for us as young seekers of purpose? It highlights a profound truth—that our identities are not confined by our physical forms or the stages of life we experience. Instead, we are eternal beings who are constantly evolving.\"\n",
    "\n",
    "    [Scene Transition - Soft Music Playing]\n",
    "\n",
    "    [Cut to a Story Animation - A young man named Arjun in a busy city]\n",
    "\n",
    "    Host (Voiceover): \"Let me share a relatable story. Meet Arjun. Like many of us, he was once full of dreams and aspirations. He excelling in school, pursuing a career in engineering. But as the years passed, he faced a crossroads. As the pressure mounted, he began to question his identity.\n",
    "\n",
    "    (Visuals show Arjun overwhelmed by societal expectations, with people pushing him in different directions.)\n",
    "\n",
    "    He felt distinct phases of life pulling at him: childhood dreams, youthful ambitions, and the looming responsibilities of adulthood. The changing seasons of his life left him confused and wondering if he had lost his true self.\"\n",
    "\n",
    "    [Cut back to Host, empathetic tone]\n",
    "\n",
    "    Host: \"Have you ever felt like Arjun? It’s a dilemma we all face, especially in today's fast-paced world where expectations can cloud our true identity. But just like our verse suggests, we should recognize that these changes don’t define us. They are simply part of the journey.\"\n",
    "\n",
    "    [Scene Transition - Calm Music Playing while Host meditates]\n",
    "\n",
    "    Host: (Speaking gently) \"Let’s take a moment to reflect. When we are sad, does that sadness define us? Or when we achieve success, do we become defined solely by that success? The answer isn't as straightforward as it seems. Here’s the catch: our essence is beyond these transient states. Like the body, our identities are fluid.\"\n",
    "\n",
    "    [Cut to Visuals of Nature - flowing rivers, trees shedding leaves, etc.]\n",
    "\n",
    "    Host Voiceover: \"Imagine the endless cycle of nature—the changing seasons, the growth, the decay, and rebirth. Just like the leaves that drop to make way for new growth, our experiences contribute to our spiritual evolution.\"\n",
    "\n",
    "    [Cut back to Host - Inviting and Warm Tone]\n",
    "\n",
    "    Host: \"Just as the wise who understand the transformation of the soul remain unshaken, we, too, can cultivate that wisdom to rise above the chaos of change. Recognize your true essence—beyond the body, the roles, the titles. Understand that your spirit is eternal.\"\n",
    "\n",
    "    [Scene Transition - Soft Inspirational Music Begins]\n",
    "\n",
    "    Host: (Passionately) \"So how can we embody this truth in our daily lives? Here’s a small exercise: Each day, take a few moments to meditate on who you really are. Write down what aspects of your identity are tied to transient things. Challenge yourself—what happens when you peel these layers away?\"\n",
    "\n",
    "    [Cut to host with a pad, writing ideas]\n",
    "\n",
    "    [Scene Transition - Editing Cues - Show engaging graphics of identity, layers of a person, etc.]\n",
    "\n",
    "    Host Voiceover: \"Each effort towards understanding and embracing our true self draws us closer to the realization that we are eternal souls, having a human experience. This is the wisdom that can empower you to stand tall against the adversities of life.\"\n",
    "\n",
    "    [Cut back to Host]\n",
    "\n",
    "    Host: (Concluding) \"Thank you for joining me today in this exploration of Chapter 2, Verse 13 of the Bhagavad Gita. Remember, when you feel lost in the complexities of life, return to this teachings and remind yourself that you are not just a body; you are an eternal being on a magnificent journey.\n",
    "\n",
    "    [Closing Scene - Uplifting Music Playing]\n",
    "\n",
    "    Host: \"Don’t forget to like, share, and subscribe if you found resonance in this message. And share your thoughts in the comments below. What did you find most challenging in your own journey of self-identity? Let’s connect and support each other in our spiritual quests. Until next time, stay enlightened, stay inspired!\"\n",
    "\n",
    "    [End Screen with Subscribe Button and Previous Video Suggestions]\n",
    "\n",
    "    [End of Script]\n",
    "    \"\"\"\n",
    "    \n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4b29cb9-d8d1-413a-8152-4250e2430a42",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(get_script_user_prompt('long','motivation'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1dfa60ce-9e88-4f7d-8e60-ac37a0aafc15",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_script(tone, theme, format):\n",
    "    response = openai.chat.completions.create(\n",
    "        model=MODEL,\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": get_script_system_prompt(tone,theme,format)},\n",
    "            {\"role\": \"user\", \"content\": get_script_user_prompt(format,theme)}\n",
    "          ],\n",
    "    )\n",
    "    result = response.choices[0].message.content\n",
    "    display(Markdown(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec86c436-42ae-4313-b12f-4fad42ab2227",
   "metadata": {},
   "outputs": [],
   "source": [
    "create_script('motivating','self-identity','long')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
